﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Specialized;
using IISLogAnalyzer.Model.Entities;

namespace IISLogAnalyzer.Data.Providers
{
    /// <summary>
    /// Class for getting data from IIS 6 ODBC logging database.
    /// Data will be returned in W3C log format
    /// </summary>
    public class ODBCLogDataProvider : DataProvider
    {
        private string _connectionString = string.Empty;

        public override void Initialize(string name, NameValueCollection config)
        {
            base.Initialize(name, config);

            this._connectionString = config["connectionString"];

            if (string.IsNullOrEmpty(this._connectionString))
                throw new ConfigurationErrorsException
                    ("connectionString must be set to the appropriate value");
        }



        public override List<W3CLogging> GetLogFromPeriod(DateTime startDate, DateTime endDate)
        {
            //WebLogDataContext dc = new WebLogDataContext(_connectionString);
            //List<W3CLogging> w3cresult = new List<W3CLogging>();

            //var result = from il in dc.inetlogs where il.LogTime >= startDate && il.LogTime <= endDate select il;

            //if (result != null)
            //{
            //    foreach (inetlog logItem in result)
            //    {
            //        W3CLogging w3clog = new W3CLogging();
            //        w3clog.c_ip = logItem.ClientHost;
            //        w3clog.cs_method = logItem.operation;
            //        w3clog.cs_ua = "unknown";
            //        w3clog.cs_uri_query = logItem.parameters;
            //        w3clog.cs_uri_stem = logItem.target;
            //        w3clog.cs_username = logItem.username;
            //        if(logItem.LogTime.HasValue)
            //            w3clog.date = logItem.LogTime.Value;
            //        w3clog.id = Guid.NewGuid();
            //        w3clog.s_ip = logItem.serverip;
            //        w3clog.s_port = 0;
            //        w3clog.s_referer = logItem.machine; // machine?
            //        w3clog.sc_status = logItem.servicestatus;
            //        w3clog.sc_substatus = logItem.processingtime; // get processing time from here
            //        w3clog.sc_win32_status = logItem.win32status;
            //        if (logItem.LogTime.HasValue)
            //            w3clog.time = logItem.LogTime.Value;
            //    }
            //}

            //return w3cresult;

            throw new NotImplementedException();
        }

        public override List<W3CLogging> GetAllLogData()
        {
            //WebLogDataContext dc = new WebLogDataContext(_connectionString);
            //List<W3CLogging> w3cresult = new List<W3CLogging>();

            //var result = from il in dc.inetlogs select il;

            //if (result != null)
            //{
            //    foreach (inetlog logItem in result)
            //    {
            //        W3CLogging w3clog = new W3CLogging();
            //        w3clog.c_ip = logItem.ClientHost;
            //        w3clog.cs_method = logItem.operation;
            //        w3clog.cs_ua = "unknown";
            //        w3clog.cs_uri_query = logItem.parameters;
            //        w3clog.cs_uri_stem = logItem.target;
            //        w3clog.cs_username = logItem.username;
            //        if (logItem.LogTime.HasValue)
            //            w3clog.date = logItem.LogTime.Value;
            //        w3clog.id = Guid.NewGuid();
            //        w3clog.s_ip = logItem.serverip;
            //        w3clog.s_port = 0;
            //        w3clog.s_referer = logItem.machine; // machine?
            //        w3clog.sc_status = logItem.servicestatus;
            //        w3clog.sc_substatus = logItem.processingtime; // get processing time from here
            //        w3clog.sc_win32_status = logItem.win32status;
            //        if (logItem.LogTime.HasValue)
            //            w3clog.time = logItem.LogTime.Value;

            //        // add values to result
            //        w3cresult.Add(w3clog);
            //    }
            //}

            //return w3cresult;
            throw new NotImplementedException();
        }

        public override int InsertLogData(List<W3CLogging> logList)
        {
            throw new NotSupportedException();
        }

        public override int InsertLogData(W3CLogging logEvent)
        {
            throw new NotSupportedException();
        }

        public override int DeleteLogData(W3CLogging logEvent)
        {
            throw new NotSupportedException();
        }



        public override List<AnalyzeResult> GetDataByCip()
        {
            throw new NotImplementedException();
        }

        public override List<AnalyzeResult> GetDataByUa()
        {
            throw new NotImplementedException();
        }

        public override List<AnalyzeResult> GetDataByResource()
        {
            throw new NotImplementedException();
        }



        public override void GetDataPeriod(ref DateTime startDate, ref DateTime endDate)
        {
            throw new NotImplementedException();
        }
    }
}
